package com.puppy.httploader
{
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.utils.ByteArray;

	public class PuppyLoader extends EventDispatcher
	{
		private var _resourceUrl:String;
		
		public function PuppyLoader(resourceUrl:String)
		{
			_resourceUrl = resourceUrl;
			super();
		}
		private var _rangeLoader:PuppySocketLoader;
		/**
		 *  加载文件的指定字节段
		 * @param startPoint:开始加载byte的位置
		 * @param endPoint:结束加载byte的位置
		 * 
		 */	
		public function loadBytes(startPoint:int, endPoint:int, isSeek:Boolean = false):void
		{
			var _httpHeader:URLRequestHeader = new URLRequestHeader("Range", "bytes=" + startPoint + "-" + endPoint);
			
			var _rangeRequest:URLRequest  = new URLRequest(_resourceUrl);
			_rangeRequest.requestHeaders.push(_httpHeader);
			
			if(_rangeLoader)
			{
				_rangeLoader.removeEventListener(Event.COMPLETE, rangeCompleteHandler);
				_rangeLoader.removeEventListener(PuppyEvent.PSTREAMING, puppyStreamingHandler);
				_rangeLoader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
				_rangeLoader.removeEventListener(Event.OPEN, openHandler);
				_rangeLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				_rangeLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				_rangeLoader.close(true);
				_rangeLoader = null;
			}
			
			_rangeLoader = new PuppySocketLoader(_rangeRequest);
		
			_rangeLoader.dataFormat = URLLoaderDataFormat.BINARY;
			
			_rangeLoader.addEventListener(Event.COMPLETE, rangeCompleteHandler);
			_rangeLoader.addEventListener(PuppyEvent.PSTREAMING, puppyStreamingHandler);
			_rangeLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			
			_rangeLoader.addEventListener(Event.OPEN, openHandler);
			_rangeLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_rangeLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			_rangeLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			_rangeLoader.load(_rangeRequest);
		}
		
		/**
		 * 流的方式加载 
		 * @param evt
		 * 
		 */		
		private function puppyStreamingHandler(evt:PuppyEvent):void
		{
			var curData:ByteArray = evt.rangeBytes;
			if(curData && curData.bytesAvailable)
			{
				dispatchEvent(new PuppyEvent(PuppyEvent.RANGE_DATA_STREAMING, curData));
			}
		}
		
		/**
		 * 完成一段的加载 
		 * @param evt
		 * 
		 */		
		private  function rangeCompleteHandler(evt:Event):void
		{
			//var curData:ByteArray = (evt.currentTarget as SocketURLLoader).data as ByteArray;
			dispatchEvent(new PuppyEvent(PuppyEvent.RANGE_DATA_COMPLETE));
		}

		/**
		 * 加载进度
		 * @param event
		 * 
		 */		
		private function progressHandler(event:ProgressEvent):void 
		{
			//trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
			dispatchEvent(event);
		}
		
		private function openHandler(event:Event):void {
			trace("openHandler: " + event);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void {
			trace("httpStatusHandler: " + event);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			trace("ioErrorHandler: " + event);
		}
	}
}